package com.itobase.statusDetect.Service.impl;

import com.itobase.statusDetect.Model.Camera;
import com.itobase.statusDetect.Model.Classroom;
import com.itobase.statusDetect.Repository.CameraRepository;
import com.itobase.statusDetect.Repository.ClassroomRepository;
import com.itobase.statusDetect.Service.ClassroomService;
import com.itobase.statusDetect.Util.StringUtils;
import com.itobase.statusDetect.VO.ClassroomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author htyu
 * @date 2019/2/24
 */
@Service
public class ClassroomServiceImpl implements ClassroomService {

    @Autowired
    ClassroomRepository classroomRepository;

    @Autowired
    CameraRepository cameraRepository;

    @Override
    public List<ClassroomVO> getList(String keyword){
        List<Classroom> list = classroomRepository.findAll();
        List<ClassroomVO> list_vo = new ArrayList<>();
        List<ClassroomVO> result;
        if(list.size()!=0) {
            for (Classroom classroom : list) {
                list_vo.add(classroomConvetVO(classroom));
            }
        }
        if(StringUtils.isNotEmpty(keyword)){
            String keyword_low = keyword.toLowerCase();
            result = list_vo.stream().filter(classroomVO ->
                            classroomVO.getAddress().toLowerCase().contains(keyword_low)||
                            classroomVO.getCamera().toLowerCase().contains(keyword_low))
                    .collect(Collectors.toList());
        }else {
            result = list_vo;
        }
        return result;
    }

    @Override
    public Classroom create(Classroom classroom){
        checkExist(classroom.getAddress());
        return classroomRepository.save(classroom);
    }

    @Override
    public ClassroomVO detial(String id) {
        Classroom classroom = classroomRepository.findById(id).get();
        return classroomConvetVO(classroom);
    }

    @Override
    public Classroom updateClassroom(Classroom classroom) {
        String address = classroom.getAddress();
        checkExist(address);
        Classroom classroom_new = classroomRepository.findByAddress(address);
        classroom_new.setAddress(address);
        classroom_new.setCameraID(classroom.getCameraID());
        return classroomRepository.save(classroom_new);
    }


    public ClassroomVO classroomConvetVO(Classroom classroom){
        ClassroomVO classroomVO  = new ClassroomVO();
        Camera camera = cameraRepository.findById(classroom.getCameraID()).get();
        classroomVO.setId(classroom.getId());
        classroomVO.setAddress(classroom.getAddress());
        classroomVO.setCamera(camera.getName());
        return classroomVO;
    }

    private void checkExist(String address) {
        Classroom classroom = classroomRepository.findByAddress(address);
        if (classroom!=null)
            throw new InvalidParameterException("教室名重复,请勿重复添加");
    }
}
